﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class BarPaging : Bar
    {
        /// <summary>
        /// 
        /// </summary>
        public BarPaging()
        {
            InitializeComponent();
            InitControl();
        }

        #region 组件设计器生成的代码

        private DevComponents.DotNetBar.ItemContainer pnlSize;
        private DevComponents.DotNetBar.LabelItem labelItem1;
        private DevComponents.DotNetBar.ComboBoxItem cmbSize;
        private DevComponents.DotNetBar.ItemContainer itemContainer2;
        private DevComponents.DotNetBar.ButtonItem btnFirst;
        private DevComponents.DotNetBar.ButtonItem btnPrevious;
        private DevComponents.DotNetBar.TextBoxItem txtPage;
        private DevComponents.DotNetBar.ButtonItem btnNext;
        private DevComponents.DotNetBar.ButtonItem btnLast;
        private DevComponents.DotNetBar.LabelItem lblDetial;
        private DevComponents.DotNetBar.ItemContainer pnlTip;

        /// <summary> 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitControl()
        {
            this.pnlSize = new DevComponents.DotNetBar.ItemContainer();
            this.labelItem1 = new DevComponents.DotNetBar.LabelItem();
            this.cmbSize = new DevComponents.DotNetBar.ComboBoxItem();
            this.itemContainer2 = new DevComponents.DotNetBar.ItemContainer();
            this.btnFirst = new DevComponents.DotNetBar.ButtonItem();
            this.btnPrevious = new DevComponents.DotNetBar.ButtonItem();
            this.txtPage = new DevComponents.DotNetBar.TextBoxItem();
            this.btnNext = new DevComponents.DotNetBar.ButtonItem();
            this.btnLast = new DevComponents.DotNetBar.ButtonItem();
            this.lblDetial = new DevComponents.DotNetBar.LabelItem();
            this.pnlTip = new DevComponents.DotNetBar.ItemContainer();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // banPanel
            // 
            this.AntiAlias = true;
            this.Font = new System.Drawing.Font("微软雅黑", 9F);
            this.IsMaximized = false;
            this.Items.AddRange(new DevComponents.DotNetBar.BaseItem[] {
            this.pnlSize,
            this.itemContainer2,
            this.pnlTip});
            this.Location = new System.Drawing.Point(0, 0);
            this.Name = "banPanel";
            this.Size = new System.Drawing.Size(664, 28);
            this.Stretch = true;
            this.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
            this.TabIndex = 0;
            this.TabStop = false;
            this.Text = "bar1";
            // 
            // pnlSize
            // 
            // 
            // 
            // 
            this.pnlSize.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            this.pnlSize.Name = "pnlSize";
            this.pnlSize.SubItems.AddRange(new DevComponents.DotNetBar.BaseItem[] {
            this.labelItem1,
            this.cmbSize});
            // 
            // 
            // 
            this.pnlSize.TitleStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            // 
            // labelItem1
            // 
            this.labelItem1.Name = "labelItem1";
            this.labelItem1.Text = "每页条数";
            // 
            // cmbSize
            // 
            this.cmbSize.DropDownHeight = 106;
            this.cmbSize.ItemHeight = 17;
            this.cmbSize.Name = "cmbSize";
            this.cmbSize.Text = "50";
            // 
            // itemContainer2
            // 
            // 
            // 
            // 
            this.itemContainer2.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            this.itemContainer2.Name = "itemContainer2";
            this.itemContainer2.SubItems.AddRange(new DevComponents.DotNetBar.BaseItem[] {
            this.btnFirst,
            this.btnPrevious,
            this.txtPage,
            this.btnNext,
            this.btnLast});
            // 
            // 
            // 
            this.itemContainer2.TitleStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            // 
            // btnFirst
            // 
            this.btnFirst.Name = "btnFirst";
            this.btnFirst.Text = "首页";
            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
            // 
            // btnPrevious
            // 
            this.btnPrevious.Name = "btnPrevious";
            this.btnPrevious.Text = "上一页";
            this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
            // 
            // txtPage
            // 
            this.txtPage.Name = "txtPage";
            this.txtPage.Text = "0";
            this.txtPage.WatermarkColor = System.Drawing.SystemColors.GrayText;
            // 
            // btnNext
            // 
            this.btnNext.Name = "btnNext";
            this.btnNext.Text = "下一页";
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // btnLast
            // 
            this.btnLast.Name = "btnLast";
            this.btnLast.Text = "尾页";
            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
            // 
            // lblDetial
            // 
            this.lblDetial.Name = "lblDetial";
            this.lblDetial.Text = "共{0}条，{1}页";
            // 
            // pnlTip
            // 
            // 
            // 
            // 
            this.pnlTip.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            this.pnlTip.Name = "pnlTip";
            this.pnlTip.SubItems.AddRange(new DevComponents.DotNetBar.BaseItem[] {
            this.lblDetial});
            // 
            // 
            // 
            this.pnlTip.TitleStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
            this.pnlTip.VerticalItemAlignment = DevComponents.DotNetBar.eVerticalItemsAlignment.Middle;
            // 
            // Paging2
            // 
            this.Name = "Paging2";
            this.Size = new System.Drawing.Size(664, 26);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        
        /// <summary>
        /// 
        /// </summary>
        private void InitData()
        {
            cmbSize.Items.AddRange(new object[] { 20, 50, 100 });
            cmbSize.SelectedIndex = 0;
        }

        /// <summary>
        /// 当前页改变事件
        /// </summary>
        public event DelegatePageIndexChanged PageIndexChanged = null;

        /// <summary>
        /// 总记录条数
        /// </summary>
        private int _RecordCount = 0;
        /// <summary>
        /// 获取总记录条数，该属性在初次查询到满足条件的记录总数后通过InitPage进行赋值
        /// </summary>
        [DefaultValue(0)]
        public int RecordCount { get { return _RecordCount; } }

        /// <summary>
        /// 总页数
        /// </summary>
        [DefaultValue(0)]
        public long PageCount { get; private set; }

        /// <summary>
        /// 当前页--从1开始
        /// </summary>
        [DefaultValue(0)]
        public long PageIndex { get; private set; }

        /// <summary>
        /// 每页的记录条数
        /// </summary>
        [DefaultValue(20)]
        public int PageSize
        {
            get
            {
                int size = 20;
                if (!string.IsNullOrWhiteSpace(cmbSize.Text))
                {
                    int.TryParse(cmbSize.Text, out size);
                }
                return size;
            }
            set
            {
                cmbSize.Text = value.ToString();
            }
        }

        /// <summary>
        /// 获取当前页开始的序号
        /// </summary>
        public long StartIndex
        {
            get { return (PageIndex - 1) * PageSize + 1; }
        }

        /// <summary>
        /// 获取当前页结束的序号，从1开始
        /// </summary>
        public long EndIndex
        {
            get { return PageIndex * PageSize; }
        }

        /// <summary>
        /// 获取或设置是否显示记录数提示
        /// </summary>
        public bool TipVisiable
        {
            get { return pnlTip.Visible; }
            set { pnlTip.Visible = value; }
        }

        /// <summary>
        /// 获取或设置是否显示每页的记录条数
        /// </summary>
        public bool SizeVisiable
        {
            get { return pnlSize.Visible; }
            set { pnlSize.Visible = value; }
        }

        /// <summary>
        /// 是否已经初始化
        /// </summary>
        private bool isInit = false;

        /// <summary>
        /// 
        /// </summary>
        private int _allcount = 0;

        /// <summary>
        /// 初始化分页控件。在开始查询之前调用。
        /// 例：点击查询按钮
        /// 1.调用page分页控件.Init();
        /// 2.查询实现的方法...
        /// </summary>
        public void Init()
        {
            isInit = false;
            _RecordCount = 0;
            PageCount = 0;
            PageIndex = 1;
            SetEnable();
        }

        /// <summary>
        /// 设置查询记录的总数。
        /// </summary>
        /// <param name="count">查询记录的总数</param>
        /// <param name="isOnlyCount">
        /// 第一次查询时，同时返回记录总数和第一页的记录信息：false；   
        /// 第一次查询时，只放回记录总数：true；
        /// </param>
        /// <param name="allcount">扩展的要显示的记录总数，如count为以选择的总数时则allcount为所有记录的总数</param>
        public void SetRecordCount(int count, bool isOnlyCount, int allcount = 0)
        {
            if (isInit) return;
            isInit = true;
            PageIndex = 1;
            _RecordCount = count;
            _allcount = allcount;
            PageCount = (int)Math.Ceiling(_RecordCount * 1.0 / PageSize);
            if (isOnlyCount)
            {
                SetPageChanged();
            }
            else
            {
                SetEnable();
            }
        }

        /// <summary>
        /// 页码改变，触发分页事件
        /// </summary>
        private void SetPageChanged()
        {
            SetEnable();
            if (PageIndexChanged != null)
            {
                PageEventArgs args = new PageEventArgs();
                args.PageIndex = PageIndex;
                args.PageSize = PageSize;
                PageIndexChanged(this, args);
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            SetPageChanged();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            PageIndex = PageIndex - 1;
            SetPageChanged();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            PageIndex = PageIndex + 1;
            SetPageChanged();
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, EventArgs e)
        {
            PageIndex = PageCount;
            SetPageChanged();
        }

        /// <summary>
        /// 查询按钮是否可用
        /// </summary>
        private void SetEnable()
        {
            btnFirst.Enabled = PageIndex > 1;
            btnPrevious.Enabled = PageIndex > 1;
            btnNext.Enabled = PageCount > PageIndex;
            btnLast.Enabled = PageCount > PageIndex;
            txtPage.Enabled = PageCount > 1;
            txtPage.Text = _RecordCount > 0 ? PageIndex.ToString() : "0";
            lblDetial.Text = string.Format("共{0}条，{1}页", RecordCount, PageCount);
            if (_allcount > 0)
            {
                lblDetial.Text = string.Format("共{0}/{1}条，{2}页", RecordCount, _allcount, PageCount);
            }
            else
            {
                lblDetial.Text = string.Format("共{0}条，{1}页", RecordCount, PageCount);
            }
        }

        /// <summary>
        /// 设置转到第几页的值：即将页码中的全角字符转换为半角字符，并验证输入的页是否不大于总页数
        /// </summary>
        /// <param name="value"></param>
        private void SetPageToValue(string value)
        {
            if (value != "")
            {
                int page = int.Parse(txtPage.Text.Trim());
                if (page >= PageCount)
                {
                    PageIndex = PageCount;
                }
                else if (page <= 1)
                {
                    PageIndex = 1;
                }
                else
                {
                    PageIndex = page;
                }
                SetPageChanged();
            }
        }
        /// <summary>
        /// 键盘按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPageTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                SetPageToValue(txtPage.Text.Trim());
            }
            else if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


    }
}
